﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SLP
{
    public class RootItem : FolderItem
    {
        public RootItem() : base() { }
        public RootItem(string name) : base() { Name = name; }

        public new string Path {  get { return ""; } } // root items can't have a path

        public void AddChild(Item item)
        {
            Stack<string> PathItems = new Stack<string>(item.Path.Split(new char[] { '\\' }));
            int PathLevel = PathItems.Count;
            FolderItem Parent = this;
            if (PathLevel > 0)
            {
                while (PathItems.Count > 0)
                {
                    string ParentName = PathItems.Pop();
                    foreach (Item itm in Parent.Items)
                    {
                        if (itm is FolderItem)
                        {
                            if (itm.Name.Equals(ParentName))
                            {
                                Parent = (FolderItem)itm;
                                PathLevel--;
                                break;
                            }
                        }
                    }
                    // not found, append path
                    FolderItem child = new FolderItem() { Path = Parent.Path + "\\" + Parent.Name, Name = ParentName };
                    Parent.Items.Add(child);
                    Parent = child;
                }
            }
            Parent.Items.Add(item);
        }
    }
}
